package NG::Gen::Task::SellLeader;
use strict;
use warnings;
use utf8;

use NG::Sys::App;
use NG::Sys::Data;
use NG::Sys::Datetime;

our @ISA = qw(NG::Gen::Task::Task);


sub delay {
	my $self = shift;
	my $p = $self->param;
	return $p->{delay_base} + $p->{delay_per} * $self->{leader}->count_unit;
}
sub make_context {
	my $self = shift;
	my @ul;
	for my $unit(@{$self->{leader}->units->list_objects}){
		if($unit->count){
			push @ul, {
				type => copy_hash($unit->proto,"entry","type","title","img"),
				count => $unit->count,
			};
		}
	}
	return {
		units => \@ul,
	};
}

sub before_assign {
	my $self = shift;
	my $base = $self->{user}->find_base_leader;
	if($base->entry == $self->{leader}->entry){
		return "нельзя удалить этого командира";
	}
	return 0;
}
sub after_done {
	my $self = shift;
	$self->{user}->l_point($self->{user}->l_point + 1);
	$self->{user}->u_point($self->{user}->u_point + $self->{leader}->count_unit);
	if($self->{leader}->items->length){
		my $base = $self->{user}->find_base_leader;
		for my $i(@{$self->{leader}->items->list_objects}){
			$i->remove;
			$base->items->insert($i->data);
		}
	}
	$self->{leader}->remove;

	$self->refresh_user();
	return 0;
}

1;
